package com.zhangwei.day41photo;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.zhangwei.day41photo.utils.FileUtils;

import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import java.io.File;

/**
 * 相机
 * 1>直接启动相机
 * 2>指定文件启动相机
 */
@EActivity(R.layout.activity_main)
public class MainActivity extends ActionBarActivity {
    public static final int PHOTO_RESULT_CODE = 111;
    @ViewById
    Button startPhotoBtn;
    @ViewById
    Button startPhotoUriBtn;
    @ViewById
    ImageView img;
    @ViewById
    Button updateImgBtn;

    /**
     * 启动系统相机拍照
     */

    public void startPhoto() {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, PHOTO_RESULT_CODE);
    }


    public void photoToPath() {
        File file = FileUtils.getPhotoFile(this);
        if (file != null) {
            Uri uri = Uri.fromFile(file);
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
            startActivity(intent);
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PHOTO_RESULT_CODE) {
            Bitmap bitmap = (Bitmap) data.getExtras().get("data");
            if (bitmap != null) {
                img.setImageBitmap(bitmap);
            }
        }
    }

    @Click({R.id.start_photo_btn, R.id.start_photo_uri_btn, R.id.update_img_btn})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.start_photo_btn:
                startPhoto();
                break;
            case R.id.start_photo_uri_btn:
                photoToPath();
                break;
            case R.id.update_img_btn:
                startActivity(new Intent(this, UpdateActivity_.class));
                break;
        }


    }


}